<template>
	<view>
		<mumu-recorder ref='recorder' @success='handlerSuccess' @error='handlerError'></mumu-recorder>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				status: false,
				audio:''
			}
		},
		onLoad() {

		},
		methods: {
			start() {
				this.$refs.recorder.start()
			},
			stop() {
				this.$refs.recorder.stop()
			},
			async handlerSuccess(res) {
				// console.log(res)
				// let a = document.createElement('a')
				// a.href = res.localUrl
				// a.download='audio'
				// a.click()
				this.recorder = res
				const arrayBuffer = await res.data.arrayBuffer()
				const base64 = uni.arrayBufferToBase64(arrayBuffer)
				this.$request({
					url:'/api/v1/miletus/vip/speech/standardReco',
					method:'post',
					data: {
						"engSerViceType": "8k_zh", 
						"voiceFormat": "m4a", 
						"sourceType": 1, 
						data: base64
					}
				}).then(res => {
					console.log(res)
					if(!res.Result) {
						uni.showToast({
							title: '没听清您的讲话，请重试！',
							icon:'none'
						})
						this.$emit('fail')
					}else {
						this.$emit('success', res.Result)
					}
				})
			},
			handlerError(code) {
				switch (code) {
					case '101':
						uni.showToast({
							title: '当前浏览器版本较低，请更换浏览器使用，推荐在微信中打开。',
							icon:'none'
						})
						break;
					case '201':
						uni.showToast({
							title: '麦克风权限被拒绝，请刷新页面后授权麦克风权限。',
							icon:'none'
						})
						break
					case '404':
						uni.showToast({
							title: '未检测到麦克风设备',
							icon:'none'
						})
						break
					default:
						uni.showToast({
							title: '未知错误，请刷新页面重试',
							icon:'none'
						})
						break
				}
			}
		}
	}
</script>
